package com.dj.mingkong.common.util;

import com.dj.mingkong.common.annotation.Comment;
import com.dj.mingkong.common.constant.CommonConstants;
import com.dj.mingkong.common.infrastructure.base.dto.query.QueryVo;
import com.dj.mingkong.common.exception.ValidateException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;

import java.util.Collection;

public final class AssertUtils {
    private static final Logger log = LoggerFactory.getLogger(AssertUtils.class);

    private AssertUtils() {
    }

    public static void isNotNull(Object obj, String message, Object... pars) {
        if (obj == null) {
            throw new ValidateException(MessageFormatter.arrayFormat(message, pars).getMessage());
        }
    }

    public static void isNotNull(Collection<?> collection, String message, Object... pars) {
        if (collection == null || collection.isEmpty()) {
            throw new ValidateException(MessageFormatter.arrayFormat(message, pars).getMessage());
        }
    }

    public static void isNotNull(String obj, String message, Object... pars) {
        if (StringUtils.isEmpty(obj)) {
            throw new ValidateException(MessageFormatter.arrayFormat(message, pars).getMessage());
        }
    }

    public static void isNoneNull(String message, Object... objs) {
        if (objs == null) {
            throw new ValidateException(message);
        } else {
            Object[] var2 = objs;
            int var3 = objs.length;

            for (int var4 = 0; var4 < var3; ++var4) {
                Object obj = var2[var4];
                if (obj instanceof String) {
                    isNotBlank((String) obj, message);
                } else {
                    isNotNull(obj, message);
                }
            }

        }
    }

    @Comment("空白字符串'  '会抛出异常")
    public static void isNotBlank(String str, String message, Object... pars) {
        if (StringUtils.isBlank(str)) {
            throw new ValidateException(MessageFormatter.arrayFormat(message, pars).getMessage());
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    @Comment("请用isNotNull")
    public static void isNotEmpty(Collection<?> collection, String message, Object... pars) {
        if (collection == null || collection.isEmpty()) {
            throw new ValidateException(MessageFormatter.arrayFormat(message, pars).getMessage());
        }
    }

    public static void isNatureNumber(int num, String message, Object... pars) {
        if (num < 0) {
            throw new ValidateException(MessageFormatter.arrayFormat(message, pars).getMessage());
        }
    }

    /**
     * @deprecated
     */
    @Deprecated
    @Comment("请用checkQueryPageVo")
    public static void checkPageNumAndPageSize(Integer pageNum, Integer pageSize) {
        if (pageNum < 1 || pageSize < 1) {
            log.error("pageNum 或 pageSize 传参不对, pageNum:{}, pageSize:{}", pageNum, pageSize);
            pageNum = CommonConstants.DEFAULT_PAGE_NUM;
            pageSize = CommonConstants.DEFAULT_PAGE_SIZE;
            log.warn("重置之后的分页参数, pageNum:{}, pageSize:{}", pageNum, pageSize);
        }

    }

    public static void checkQueryPageVo(QueryVo queryVo) {
        if (queryVo.getPage() < 1 || queryVo.getPageSize() < 1) {
            log.error("pageNum 或 pageSize 传参不对, pageNum:{}, pageSize:{}", queryVo.getPage(), queryVo.getPageSize());
            queryVo.setPage(CommonConstants.DEFAULT_PAGE_NUM);
            queryVo.setPageSize(CommonConstants.DEFAULT_PAGE_SIZE);
            log.warn("重置之后的分页参数, pageVo:{}", queryVo);
        }

    }
}
